package com.moneywelcome.swagger;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * 接口文档配置,需配置
 *      当前项目url：    swagger.url = 如localhost:8080
 *      需扫描的包：      swagger.scanPackage = 如com.mwc.controller
 */
@Configuration
@EnableSwagger2
@Data
public class SwaggerConfig {
    @Value("${swagger.url}")
    private String url;
    @Value("${swagger.scanPackage}")
    private String scanPackage;
    @Bean
    public Docket createDocket() {
        //创建文档对象
        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        tokenPar.name("Authorization").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(true).build();
        pars.add(tokenPar.build());
        //创建文档的整体说明信息对象
        ApiInfo apiInfo = new ApiInfoBuilder()
                .version("1.0")
                .title("钱乐融融接口文档")
                .description("钱乐融融接口文档使用")
                .contact(new Contact("钱乐融融", url, "null"))
                .build()
                ;
        //把信息对象设置到文档对象上面
        return docket.apiInfo(apiInfo)
                //指定生成文档所需要的接口类扫描路径
                .select()
                //指定扫描包的范围
                .apis(RequestHandlerSelectors.basePackage(scanPackage))
                //扫描范围内所有的接口都生成
                .paths(PathSelectors.any())
                .build().globalOperationParameters(pars);
    }
}
